package com.zy.portal.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zy.common.util.CookieUtils;
import com.zy.common.util.HttpClientUtil;
import com.zy.common.util.TaotaoResult;
import com.zy.portal.service.ContentService;

@Controller
public class IndexController {
	@Autowired
	private ContentService contentService;
	@RequestMapping("/user/login")
	public TaotaoResult showLogin(String username, String password, Model model) {
		String json = HttpClientUtil
				.doGet("http://localhost:8084/user/login" + "?username=" + username + "&password=" + password);
		TaotaoResult taotaoResult = TaotaoResult.format(json);
		if (taotaoResult.getStatus() == 200) {
			String adJson = contentService.getContentList();
			model.addAttribute("ad1", adJson);
			return TaotaoResult.ok();
		} else {
			return TaotaoResult.ok("500");
		}
	}

	@RequestMapping("/index")
	public String showIndex(Model model) {
		// 得到广告数据
//		String adJson = contentService.getContentList();
//		model.addAttribute("ad1", adJson);
//		return "login";
		String adJson = contentService.getContentList();
		model.addAttribute("ad1", adJson);
		return "index";
	}

	@RequestMapping("/user/login/index")
	public String getIndex(Model model) {
		// 得到广告数据
		String adJson = contentService.getContentList();
		model.addAttribute("ad1", adJson);
		return "index";
	}
	
	@RequestMapping("/user/logout")
	public String logout(Model model,HttpServletRequest request) {
		String cookieValue = CookieUtils.getCookieValue(request, "TT_TOKEN");
		HttpClientUtil.doGet("http://localhost:8084/user/logout/"+cookieValue);
		String adJson = contentService.getContentList();
		model.addAttribute("ad1", adJson);
		return "index";
	}
}
